// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/efficiency/api/MonitorPointSiteApi'

import ComClass from '@api/ComQuery'
import TreeClass from '@api/efficiency/model/QueryMonitorPointSiteListReq'
import OneClass from '@api/efficiency/model/QueryMonitorPointSiteDetailReq'
import AddClass from '@api/efficiency/model/InsertMonitorPointSiteReq'
import EditClass from '@api/efficiency/model/UpdateMonitorPointSiteReq'
import DelClass from '@api/efficiency/model/DeleteMonitorPointSiteReq'
import PassClass from '@api/efficiency/model/BindMonitorPointSiteDeviceReq'
import RejClass from '@api/efficiency/model/UnbindMonitorPointSiteDeviceReq'
import BaseClass from '@api/efficiency/model/MonitorPointSiteDto'
import DeviceClass from '@api/efficiency/model/MonitorPointSiteDeviceDto'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const BASE = [BaseClass, 'monitorPointSiteDto']
const DEVICE = [DeviceClass, 'monitorPointSiteDeviceDto']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 结构树 —————————————————————————————— //

  async getTree(treeParam, TIP = '查询结构树') {
    const DTO = CORE.grpcDTO(TreeClass, treeParam, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryMonitorPointSiteList', DTO)
    return resp.monitorPointSiteDto || []
  },
  transTree(arr) {
    arr.sort((a, b) => a.monitorPointName.localeCompare(b.monitorPointName, 'zh-CN'))
    arr.forEach((item) => this.transTree(item.children || []))
    return arr
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  validOne(one) {
    return one.id
  },
  async getOne({ id }, TIP = '查询详情') {
    const DTO = CORE.grpcDTO(OneClass, { id })
    const resp = await CORE.grpc(TIP, CLIENT, 'queryMonitorPointSiteDetail', DTO)
    return { ...resp.monitorPointSiteDto, deviceList: resp.monitorPointSiteDeviceDto || [] }
  },
  transOne(obj) {
    let noDel = false
    obj.deviceList.forEach((item, i) => {
      item.id = item.id || (i + 1)
      item.status = item.deviceId ? 1 : 2
      if (item.deviceId) noDel = true
    })
    obj.noDel = noDel // 标识能否删除
    return obj
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'insertMonitorPointSite', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'updateMonitorPointSite', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { ids: id })
    await CORE.grpc(TIP, CLIENT, 'deleteMonitorPointSite', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 通过 —————————————————————————————— //

  transPass(one) {
    return one.device
  },
  async pass(one, TIP = '通过') {
    const DTO = CORE.grpcDTO(PassClass, one, DEVICE)
    await CORE.grpc(TIP, CLIENT, 'bindMonitorPointSiteDevice', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 拒绝 —————————————————————————————— //

  transRej(one) {
    return one.device
  },
  async rej({ id }, TIP = '拒绝') {
    const DTO = CORE.grpcDTO(RejClass, { ids: id })
    await CORE.grpc(TIP, CLIENT, 'unbindMonitorPointSiteDevice', DTO)
    return CORE.grpcOK(TIP)
  },
}
